package com.example.demo.controller;

import com.example.demo.commen.AppResult;
import com.example.demo.commen.ResultCode;
import com.example.demo.exception.ApplicationException;
import com.example.demo.model.Article;
import com.example.demo.model.ArticleReply;
import com.example.demo.model.User;
import com.example.demo.service.impl.ArticleReplyServiceImpl;
import com.example.demo.service.impl.ArticleServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "回复接口")
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Autowired
    ArticleReplyServiceImpl articleReplyService;
    @Autowired
    ArticleServiceImpl articleService;

    @ApiOperation("帖子回复")
    @RequestMapping(method = RequestMethod.POST,value = "/articleReply")
    public AppResult addArticleReply(HttpServletRequest request,
                                     @ApiParam("帖子ID") @RequestParam("id") Long articleId,
                                     @ApiParam("回复内容")@RequestParam("content") String content){

        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("userInfo");
        if(user.getState()==1){
            throw new ApplicationException("用户被禁言");          //规定禁言不可发数据
        }
        if(articleId==null||articleId<=0||!StringUtils.hasLength(content)){
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }

        ArticleReply articleReply = new ArticleReply();

        Article article = articleService.selectArticleById(articleId);
        if(article==null||article.getId()<=0||article.getState()==1||article.getDeleteState()==1){
            throw new ApplicationException("帖子不存在");
        }
        articleReply.setArticleId(articleId);
        articleReply.setContent(content);
        articleReply.setPostUserId(user.getId());
        articleReplyService.createArticleReply(articleReply);
        return AppResult.success("回复成功");
    }

    @ApiOperation("显示对应帖子的回复内容")
    @RequestMapping(method = RequestMethod.GET,value = "/articleReplyById")

    public AppResult<List<ArticleReply>> getArticleReplyById(@ApiParam("帖子Id")
                                                              @RequestParam(value = "articleId",required = true) Long articleId){

        if(articleId==null||articleId<=0){
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }
        Article article = articleService.selectArticleById(articleId);
        if(article==null||article.getId()<=0||article.getState()==1||article.getDeleteState()==1){
            throw new ApplicationException("帖子发生异常");
        }

        List<ArticleReply> lists = articleReplyService.getListArticleReplyByArticleId(articleId);
        if(lists==null){
            throw new ApplicationException("此贴无回复");
        }
        return AppResult.success(lists);
    }
}
